package com.zcy.auth.service.impl;

import com.zcy.auth.domain.Role;
import com.zcy.auth.dto.RoleDTO;
import com.zcy.auth.mapper.RoleMapper;
import com.zcy.auth.mapper.RolePermissionMapper;
import com.zcy.auth.service.IRoleService;
import com.zcy.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author lucker
 * @since 2023-12-03
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    @Transactional
    @Override
    public void saveRole(RoleDTO roleDTO) {
        if (roleDTO.getId() == null) {
            roleMapper.add(roleDTO);
        } else {
            rolePermissionMapper.deleteByRoleId(roleDTO.getId());
        }
        if (roleDTO.getPermissionSnList() != null && !roleDTO.getPermissionSnList().isEmpty()) {
            rolePermissionMapper.addBatch(roleDTO);
        }
    }

    @Transactional
    @Override
    public int deleteById(Serializable id) {
        rolePermissionMapper.deleteByRoleId(id);
        return roleMapper.deleteById(id);
    }
}
